package org.test.string;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;

/*******************************************************************************
 * 字串和collection的转换 StringAndCollection
 * 
 * @author：denggm@asiainfo-linkage.com
 * @Nov 5, 2012 10:01:07 AM
 * 
 * Copyright asiainf-linkage Corporation 2012
 */
public class StringAndCollectionUtil {

	Logger log = Logger.getLogger(this.getClass());

	/**
	 * 
	 * @param msgString
	 *            形如 "a=1,b=2,c=3" 类似形式
	 * @param regex
	 *            形如"," ";" map
	 * @return 
	 * Map 形如{b=2, c=3, a=1}
	 * @author：denggm@asiainfo-linkage.com
	 * @Nov 5, 2012 10:25:28 AM
	 */
	public static Map converStringToMap(String msgString, String regex) {
		String[] stringArray = msgString.split(regex);

		Map map = new HashMap();
		for (String string : stringArray) {
			String key = string.substring(0, string.indexOf('='));
			String value = string.substring(string.indexOf('=') + 1);
			map.put(key, value);
		}
		return map;
	}

	/**
	 * @param msgString
	 *            形如 "a1,b2,c3" 形式
	 * @param regex
	 *            形如","
	 * @return Map
	 * @author：denggm@asiainfo-linkage.com
	 * @Nov 5, 2012 10:14:49 AM
	 */
	public static List converStringToList(String msgString, String regex) {
		String[] stringArray = msgString.split(regex);
		List list = new ArrayList();
		CollectionUtils.addAll(list, stringArray);
		return list;
	}
}
